package com.haixue.android.academy.activity;

import android.academy.haixue.com.haixue.R;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.haixue.android.academy.adapter.CategoryAdapter;
import com.haixue.android.academy.domain.Categorys;
import com.haixue.android.academy.domain.User;
import com.haixue.android.academy.utils.IntentUtils;

import java.io.Serializable;

import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnItemClick;

public class SelectCategoryActivity extends BaseSelectCategoryActivity implements Animation.AnimationListener {
    @InjectView(R.id.ll_footer)
    LinearLayout ll_footer;

    @InjectView(R.id.lv_category_data)
    ListView lv_category_data;

    @InjectView(R.id.ll_category_container)
    LinearLayout ll_category_container;

    private User user;
    private TranslateAnimation animationIn;
    private TranslateAnimation animationOut;
    private Animation animationAlphaIn;
    private Animation animationAlphaOut;
    private CategoryAdapter categoryAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_category);
    }


    @OnItemClick(R.id.lv_category_data)
    void OnItemClick(int position) {
//        Log.e(TAG, "on item click " + testAdapter.getItem(position));

        Intent intent = new Intent();
        Categorys data = categoryAdapter.getData(position);
        intent.putExtra("data",data.getId());
        setResult(IntentUtils.DEFAULT_REQUEST_CODE, intent);
        Toast.makeText(this, "on item click " + data.getName(), Toast.LENGTH_SHORT).show();
        closeMenu();
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        Serializable data = getIntent().getSerializableExtra("data");
        if (data!=null){
            user = (User) data;
            categoryAdapter = new CategoryAdapter(getActivity());
            categoryAdapter.setDatas(user.getCategory());
            lv_category_data.setAdapter(categoryAdapter);
        }

        animationIn = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
                -1.0f, Animation.RELATIVE_TO_SELF, 0);

        animationIn.setInterpolator(new DecelerateInterpolator());

        animationIn.setDuration(300);

        animationOut = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, -1.0f);

        animationOut.setDuration(300);

        animationOut.setFillEnabled(true);

        animationOut.setFillAfter(true);

        animationAlphaIn = (Animation) AnimationUtils.loadAnimation(this,
                R.anim.animation_alpha_in);
        animationAlphaOut = (Animation) AnimationUtils.loadAnimation(this,
                R.anim.animation_alpha_out);

        animationAlphaOut.setFillEnabled(true);
        animationAlphaOut.setFillAfter(true);

        ll_category_container.startAnimation(animationAlphaIn);
        lv_category_data.startAnimation(animationIn);
    }

    @OnClick(R.id.ll_footer)
    public void closeMenu(){
        ll_category_container.clearAnimation();
        lv_category_data.clearAnimation();
        animationOut.setAnimationListener(this);
        lv_category_data.startAnimation(animationOut);
        ll_category_container.startAnimation(animationAlphaOut);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键
            closeMenu();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        finish();
        overridePendingTransition(0, 0);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    @Override
    public void onSelectCategoryClick(View view) {
        closeMenu();
    }
}
